/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 * This is an old version without wizard. use to load pcp data
 * @Original Author: jin Chen
 * @date: Oct 23, 2003$
 * @version: 1.0
 */
package edu.psu.geovista.app.dataloader.v1.components;

import edu.psu.geovista.app.dataloader.v1.config.model.Pcpdata;
import edu.psu.geovista.app.dataloader.v1.gui.DataSelectPane;
import edu.psu.geovista.ui.dataloader.common.FileLoadingPane;
import edu.psu.geovista.ui.dataloader.event.LoadDataEvent;
import edu.psu.geovista.ui.dataloader.event.LoadDataListener;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;


public class PcpDataLoader extends SingleDataLoader /*implements TSDataListener*/{
    public static final boolean DEBUG=false;
    public static final String ObservationFile="ob data";//"Observations";//"ob data";
    public static final String ObMetaFile="meta data";//"Metadata";//"meta data";
    public static final String ShapeFile="shape data";//"Shape File";//"shape data";
    //labels
    public static final String ObservationLabel="Observations";//"Observations";//"ob data";
    public static final String ObMetaLabel="Metadata";//"Metadata";//"meta data";
    public static final String ShapeLabel="Shape File";//"Shape File";//"shape data";

    protected  JCheckBox projectCheck;//=new JCheckBox("Projected Data");
    public PcpDataLoader(NCIDataSource dataSource) {
        super(dataSource);
        TitledBorder border = new TitledBorder("PCP Data");
        border.setTitleColor(TEXT_COLOR );
        this.setBorder(border);
    }
    /**
     * set pcp path info based on project setting
     * @param datainfo
     */
    public void setDataInfo(Pcpdata  datainfo){

        if (datainfo!=null) {
            //PcpdataType pcpdata=project.getPcpdata();
            //2. projected
            boolean projected=datainfo.isShapeProjected();
            projectCheck.setSelected(projected);
            dataAssembler.setProjectedData(projectCheck.isSelected() );// 
            super.setDataInfo(datainfo);
            /*
            //1. data path
            Properties paths= ConfigureProcessor.getPaths(pcpdata);
            this.setFileLoaderValues(paths);

            //3. load data, if path not correct =>exception
            this.loadDataInfo();
            //4. if data is loaded, selected attributes
            String [] attrs=ConfigureProcessor.getSelectAttributeNames(pcpdata);
            this.dataList.setSelectedAttributes(attrs); */
        } else {
            this.setAllFileLoaderValues("");
        }
    }
    /**
     * put data information into datainfo
     * @param datainfo
     */
    public void fillProjectInfo(Pcpdata  datainfo){

        super.fillProjectInfo(datainfo);
        boolean projected=this.projectCheck.isSelected();
        datainfo.setShapeProjected(projected);
        //ModelUtils.setDataProjected(datainfo,projected);

    }
   /* public void doApply() {
      this.dataList.doApply() ;
      getProjectInfo();
    }*/
    public void initComponents(){
        //ListPane
        this.dataAssembler.setDataReceiverType(edu.psu.geovista.app.dataloader.v1.components.DataAssembler.DataReceiver_PCP );   //It means the dataAssembler(and thus this dataloader) will send PCP data
        dataList=new DataSelectPane(dataAssembler);
        setupListPane(dataList);

        //pathPane
        String[] names={ObservationFile,ObMetaFile,ShapeFile};
        dataPathPane=new FileLoadingPane(names);
        String [] labels={ ObservationLabel,  ObMetaLabel,  ShapeLabel};
      /* URL classLocation = JavaCoreUtils.getClassLocation(FileLoadingPane.class);
       String s = classLocation.toString();*/
       dataPathPane.setFileSrcLabels(labels);


        ldl=new LoadDataListener(){
             public void loadData(LoadDataEvent evt) {

                 try {
                     PcpDataLoader.this.loadDataInfo();
                 } catch (Exception e) {
                     if(DEBUG)
                        e.printStackTrace();
                 }

             }
        };
        dataPathPane.addLoadDataListner(ldl);


       projectCheck=new JCheckBox("Projected Data");
       final JCheckBox projectCheck=this.projectCheck;

        projectCheck.setSelected(this.dataAssembler.isProjectedData() );
        projectCheck.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        dataAssembler.setProjectedData(projectCheck.isSelected() );
                    }
                });
        this.addControlComponent(projectCheck);
    }

    /**
     * load data information
     * @return if false, means the process is not successful
     */
    public    boolean loadDataInfo() throws Exception{
                  FileLoadingPane flp=dataPathPane;//(FileLoadingPane) evt.getSource() ;
                 String obF=flp.getFileLoaderValue(ObservationFile);
                 String metaF=flp.getFileLoaderValue(ObMetaFile);
                 String shpF=flp.getFileLoaderValue(ShapeFile);

                 File obf=new File(obF);

        /*File shp=null;
        if (shpF!=null) {
            shp = new File(shpF);
        }*/
        if((obf!=null&&obf.exists())/*&&
                         (shp!=null&&shp.exists())*/ ){
                     return firstProcessFiles(obF,metaF,shpF);
                 }
                 else{
                     return false;
                 }
                 //return firstProcessFiles(obF,metaF,shpF);

    }

    /**
     * Extend by subclass
     * setup listpane
     * @return
     */
    protected   DataSelectPane setupListPane(DataSelectPane  dataP){

        //dataP.setMainSectionName("Pcp");
        dataP.setLeftSectionName("Data available");
        dataP.setRightSectionName("Data for analysis");
        return   dataP;
    }
    /*******************************************************************************************************
     * DataEvent
     * Data loaded by PcpDataLoader should have same number of record as that loaded bt TsDataLoader
     * Thus, the dataSource of each DataLoader must listen to each other to keep track the numOfRecord of the other
     * if the numberOfRecords are not equal, must info user
     *******************************************************************************************************/
    /*public void tsDataChange(TSDataEvent e) {
        AppDataModel dataModel=e.getDataModel() ;
        int numOfRec=dataModel.getNumRecords() ;
        this.dataAssembler.setExpectedNumOfRecord(numOfRec);
    }*/
    public DataSelectPane getDataList() {
        return dataList;
    }

    public FileLoadingPane getDataPathPane() {
        return dataPathPane;
    }

    public JCheckBox getProjectCheck() {
        return projectCheck;
    }
     /**
      * A name of path. e.g.  obMeta, or shp....
      * @param fileSrcNames
      */
     public void setPathNames(String[] fileSrcNames) {
         this.dataPathPane.setFileSrcNames(fileSrcNames);
     }
   


}
